import Component from "../Component.js";
import ServiceUtils from "../../experience/other/shanxi/ServiceUtils.js"
import Storage from "../../experience/utils/Storage.js";
// 定时更新 本地indexDb中的device表格,并重新渲染设备位置
export default class DeviceTimerRefresh extends Component {
    constructor() {
        super();
    }

    template() {
    }

    async control() {
        this.ServiceUtils = new ServiceUtils()
        this.Storage = new Storage()
        this.timerRefresh()
        this.refreshValve = true
        this.emitter.on('refresh-valve',() => {
            this.refreshValve = this.emitter.params[0]
        })
    }

    // 定时更新 deviceDb
    timerRefresh() {
        // 用更短的时间进行人员位置更新
        setInterval(async () => {
            if(this.refreshValve) {
                await this.drawer._Shanxi_Device_mStaff()
            }
        }, 60000 * 2)

        // 每次启动项目后，先进行一次更新
        this.updateDevice()
        setInterval(async () => {
            if(this.refreshValve) {
                await this.updateDevice()
                // 更新 目录树的颜色效果进行更新
                this.emitter.emit('device-tree-refresh')
            }
        },60000 * 3)
    }

    async updateDevice() {
        const current_metaId = this.utils.getUrlParamer('meta_id')
        const { data:rdata } = await this.api.shanxiDevicePosGet(current_metaId)
        const {device_pos_infos:current_devices_pos} = rdata
        await this.db.refresh('device',current_devices_pos)
        await this.drawer.shanxi_device_refresh()
    }

}